package com.data.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.annotation.ApiLogDetail;
import com.common.base.BaseEnum;
import com.common.exception.BaseException;
import com.common.vo.RankingListVo;
import com.data.core.entry.RankingList;
import org.springframework.beans.BeanUtils;
import org.springframework.data.annotation.Transient;
import org.springframework.stereotype.Service;
import com.data.core.dao.RankingListMapper;
import com.data.core.service.RankingListService;
import javax.annotation.Resource;
import java.util.Objects;

/**
 * @version: V1.0
 * @author: szw
 * @className: RankingListServiceImpl
 * @packageName: RankingListServiceImpl
 * @description: 排行榜接口实现
 * @data: 2021-1-23
 **/
@Service
public class RankingListServiceImpl extends ServiceImpl<RankingListMapper, RankingList> implements RankingListService {

    @Resource
    private RankingListMapper rankingListMapper;

    /**
     * 保存排行榜数据
     * @param rankingListVo
     * @return
     * @throws Exception
     */
    @Transient
    @Override
    @ApiLogDetail(detail = "核心服务-保存排行榜数据",operationType=BaseEnum.INSERT)
    public int save(RankingListVo rankingListVo) throws Exception {
        if (Objects.isNull(rankingListVo)){
            throw new BaseException(BaseEnum.RANKING_DATA_IS_NULL);
        }
        try {
            return saveToUpdate(rankingListVo,true);
        }catch (Exception e){
            throw new BaseException(BaseEnum.SAVE_RANKING_DATA_IS_ERROR);
        }
    }

    /**
     * 更新排行榜数据
     * @param rankingListVo
     * @return
     * @throws Exception
     */
    @Transient
    @Override
    @ApiLogDetail(detail = "核心服务-更新排行榜数据",operationType=BaseEnum.UPDATE)
    public int update(RankingListVo rankingListVo) throws Exception {
        if (Objects.isNull(rankingListVo)){
            throw new BaseException(BaseEnum.RANKING_DATA_IS_NULL);
        }
        try {
            return saveToUpdate(rankingListVo,false);
        }catch (Exception e){
            throw new BaseException(BaseEnum.UPDATE_RANKING_DATA_IS_ERROR);
        }
    }

    /**
     * 查询用户排行数据
     * @param userId
     * @return
     */
    @ApiLogDetail(detail = "核心服务-查询用户排行数据",operationType=BaseEnum.SELECT)
    @Override
    public RankingListVo findRankingListByUserId(Long userId) {
        if (null==userId){
            throw new BaseException(BaseEnum.USER_ID_IS_NULL);
        }
        try {
            RankingListVo rankingListVo = new RankingListVo();
            QueryWrapper<RankingList> queryWrapper =new QueryWrapper<>();
            queryWrapper.eq("user_id",userId);
            RankingList rankingList= rankingListMapper.selectOne(queryWrapper);
            if (Objects.isNull(rankingList)){
                return  null;
            }
            BeanUtils.copyProperties(rankingList,rankingListVo);
            return rankingListVo;
        }catch (Exception e){
            throw new BaseException(BaseEnum.FIND_USER_RANKING_DATA_IS_ERROR);
        }
    }

    /**
     * 分页查询排行榜数据
     * @param page
     * @param rankingListVo
     * @return
     * @throws Exception
     */
    @Override
    @ApiLogDetail(detail = "核心服务-分页查询排行榜数据",operationType=BaseEnum.SELECT)
    public IPage<RankingList> rankingListPage(Page<RankingList> page, RankingListVo rankingListVo) throws Exception {
        if (Objects.isNull(rankingListVo)){
            throw new BaseException(BaseEnum.RANKING_DATA_IS_NULL);
        }
        try {
            QueryWrapper<RankingList> queryWrapper = new QueryWrapper<>();
            return  rankingListMapper.selectPage(page,queryWrapper);
        }catch (Exception e){
            throw new BaseException(BaseEnum.FIND_RANKING_DATA_IS_ERROR);
        }
    }

    /**
     * 更新、保存排行数据
     * @param rankingListVo
     * @param flag true 保存 false 更新
     * @return
     */
    private int saveToUpdate(RankingListVo rankingListVo, Boolean flag){
        RankingList rankingList = new RankingList();
        BeanUtils.copyProperties(rankingListVo,rankingList);
        if (flag){
            return rankingListMapper.insert(rankingList);
        }
        return  rankingListMapper.updateById(rankingList);
    }
}
